from logtool import *

#------------------------------------------------
#***** 准备阶段 *****

# 1.从原始log找出dir1,也就是xt-xcc所在的目录的路径
dir1_string = r"C:\usr\xtensa\XtDevTools\install\tools\RG-2018.9-win32\XtensaTools\bin"

# 2.从原始log找出--xtensa-system=后面跟随的路径
dir2_string = r"C:/usr/xtensa/XtDevTools/install/builds/RG-2018.9-win32/qingwei_config_0125/config"

# 3.从原始log找出 trunk所在的路径
trunk = 'D:/cdcdcd/trunk'

# 4.从原始log找出include的部分
para_include = '-I "D:/cdcdcd/trunk/board/tx210/include" -I "D:/cdcdcd/trunk/core" -I "D:/cdcdcd/trunk/driver/tx210/inc" -I "D:/cdcdcd/trunk/driver/tx210/src/spi_sd/Fatfs" -I "D:/cdcdcd/trunk/lib" -I "D:/cdcdcd/trunk/lib/aie/aieDriver/Inc" -I "D:/cdcdcd/trunk/lib/aie/aieModel/Inc" -I "D:/cdcdcd/trunk/lib/aie/aieModel/Inc/ModelCfg" -I "D:/cdcdcd/trunk/lib/aie/aieModel/lib_lstm" -I "D:/cdcdcd/trunk/lib/aie/aieModel/ZkyDecoder" -I "D:/cdcdcd/trunk/lib/aie/aieModel/ZkyNieDecoder" -I "D:/cdcdcd/trunk/lib/aie/aieModel/ZkyNieFstTdnnDecoder" -I "D:/cdcdcd/trunk/lib/aie/aieModel/ZkyNieVadTdnnDecoder" -I "D:/cdcdcd/trunk/lib/aie/Algo" -I "D:/cdcdcd/trunk/lib/aie/Algo/AlangoDNS" -I "D:/cdcdcd/trunk/lib/aie/Algo/basic" -I "D:/cdcdcd/trunk/lib/aie/Algo/IIRFilter" -I "D:/cdcdcd/trunk/lib/aie/Algo/lib/base" -I "D:/cdcdcd/trunk/lib/aie/Algo/lib/component/ada" -I "D:/cdcdcd/trunk/lib/aie/Algo/lib/component/AGC" -I "D:/cdcdcd/trunk/lib/aie/Algo/lib/component/FFT" -I "D:/cdcdcd/trunk/lib/aie/Algo/lib/component/nnba" -I "D:/cdcdcd/trunk/lib/aie/Algo/lib/component/SNS" -I "D:/cdcdcd/trunk/lib/aie/Algo/lib/component/VAD" -I "D:/cdcdcd/trunk/lib/aie/Algo/lib_fusion" -I "D:/cdcdcd/trunk/lib/aie/Algo/lib_tsmfir" -I "D:/cdcdcd/trunk/lib/aie/Algo/math" -I "D:/cdcdcd/trunk/lib/aie/Algo/SoundpDns" -I "D:/cdcdcd/trunk/lib/aie/Algo/ZkyAec" -I "D:/cdcdcd/trunk/lib/aie/Algo/ZkyAgcSujy" -I "D:/cdcdcd/trunk/lib/aie/Algo/ZkyBeamForming" -I "D:/cdcdcd/trunk/lib/aie/Algo/ZkyBeamForming0angleMNR" -I "D:/cdcdcd/trunk/lib/aie/Algo/ZkyBeamForming0angleMNR2" -I "D:/cdcdcd/trunk/lib/aie/Algo/ZkyBeamForming_0" -I "D:/cdcdcd/trunk/lib/aie/Algo/ZkyDualMicBFMNR" -I "D:/cdcdcd/trunk/lib/aie/Algo/ZkyDualMicBfRlsMnr" -I "D:/cdcdcd/trunk/lib/aie/Algo/ZkyDualMicDCDR" -I "D:/cdcdcd/trunk/lib/aie/Algo/ZkyDualMicRLS" -I "D:/cdcdcd/trunk/lib/aie/Algo/ZkyDualMicRLSMNR" -I "D:/cdcdcd/trunk/lib/aie/Algo/ZkyGruNN" -I "D:/cdcdcd/trunk/lib/aie/Algo/ZkyGruNN_DMic" -I "D:/cdcdcd/trunk/lib/aie/Algo/ZkyMNR" -I "D:/cdcdcd/trunk/lib/aie/Algo/ZkyReSample" -I "D:/cdcdcd/trunk/lib/aie/Algo/ZkySkvAgc" -I "D:/cdcdcd/trunk/lib/aie/Algo/ZkySkvAgcWxb" -I "D:/cdcdcd/trunk/lib/aispeech" -I "D:/cdcdcd/trunk/lib/common" -I "D:/cdcdcd/trunk/lib/dns" -I "D:/cdcdcd/trunk/lib/image" -I "D:/cdcdcd/trunk/lib/image/model" -I "D:/cdcdcd/trunk/lib/include" -I "D:/cdcdcd/trunk/lib/speech" -I "D:/cdcdcd/trunk/lib/speech/model" -I "D:/cdcdcd/trunk/lib/speech/model/cmd_fengshan" -I "D:/cdcdcd/trunk/lib/speech/model/cmd_taideng" -I "D:/cdcdcd/trunk/lib/speech/model/xiaoe_v6_0" -I "D:/cdcdcd/trunk/lib/tsm_trunk/base" -I "D:/cdcdcd/trunk/lib/tsm_trunk/component/nnba" -I "D:/cdcdcd/trunk/project/common" -I "D:/cdcdcd/trunk/project/scenario_earphone_cmd" -I "D:/cdcdcd/trunk/util" -I "D:/cdcdcd/trunk/util/sd_record_util"'

# 5.从原始log找出宏的部分
para_hong = '-DPROC_qingwei_config_0125 -DCONFIG_qingwei_config_0125 -DCONFIG_AUDIO_8K -DCONFIG_RTL_FULLMASK -DCONFIG_CHIP_TX210 -DCONFIG_CPU=FUSION_F1 -DCONFIG_SYSTEM_VERSION="akk" -DCONFIG_AUDIO_MODE_REPLAY -DCONFIG_VAD_MODE_BUFFER -DCONFIG_VAD_MODE_RING_BUFFER -DCONFIG_DEBUG_VAD_SLEEP_ONLY -DCONFIG_SMOOTH_ZKY_NIE -DCONFIG_DEBUG_INITIALIZATION -DCONFIG_VAD_ADC_WHILE_SLEEP -DCONFIG_SMOOTH_ZKY_VARIETY_THRESHOLD -DCONFIG_AIE_DNN_RES_NEW_FIFO -DCONFIG_AIE_FM_8K_TEST_TEAM_USE_HONG -DCONFIG_DRIVER_I2C -DDEBUG_UART_PB0102 -DCONFIG_CHIP_PACK_WLCSP -DCONFIG_VAD_CMD -DI2C_SLAVE_MODE -DI2C_SLAVE -DCONFIG_RELEASE_'

#***** 准备阶段结束 *****
#------------------------------------------------


para_include_withpp = para_include.replace(trunk,'%ttk%')

#这部分common部分一般没有变化
para_common = '-fsigned-char -std=c99 -ffunction-sections -fdata-sections -fmessage-length=0'

#以下不需要需修改，其中ABC是尾巴部分的替换
para_dir1 = dir1_string.replace('\\',"\\\\")
para_dir2 = dir2_string
para_name_part = r'-DCONFIG_SYSTEM_VERSION=".*?"'   #版本号标志
para_A1 = r"%ttk%/bin/qingwei_config_0125/Debug/objfiles.list -o %ttk%/bin/qingwei_config_0125/Debug/trunk"
para_A2 = r"%outdirb%/objfiles.list -o %outdirb%/trunk"
para_B1 = r"cmd /c xt-objcopy"
para_B2 = r"%dir1%\xt-objcopy.exe".replace('\\',"\\\\")
para_C1 = r"bin/qingwei_config_0125/Debug/trunk trunk.bin"
para_C2 = r"%outdirb%/trunk %outdirb%/trunk.bin"


#新头部
new_head = [r'set dir1=' + dir1_string + '\n',
        r'set common1='+ para_common  + '\n',
        r'set ttk=%1'  + '\n',
        r'set ttkb=%ttk:\=/%' + '\n',
        r'set outdir=%2' + '\n',
        r'set release_name=%3' + '\n',
        r'set outdird=%outdir:\=/%' + '\n',
        r'set outdirb=%outdird: =%' + '\n',
        r'set include=' + para_include_withpp + '\n',
        r'set hong='+para_hong + '\n',
        r'set dir2=' + dir2_string + '\n',
        '\n',
        ]

#新尾巴，一般不需要修改
new_tail = [
        r'dir /b/s %outdir% >good.list' + '\n',
        r"sed -i 's/\\\/\//g' good.list" + '\n',
        r'TYPE good.list | FIND "" /V > %outdir%\objfiles.list' + '\n',
        ]


files = os.listdir('inbox')
allnum = len(files)

def getlines_num(filepath):
    with open(filepath,'r') as f:
        alllines=f.readlines()
    line_number = len(alllines)
    return line_number

for indexx,file in enumerate(files):
    print(f"处理第{indexx+1}个文件/共{allnum}个")
    file_path = os.path.join('inbox',file)
    
    # 删除头部和尾部的无用信息
    process_HeadLines(file_path,1,'Cleaning the build output folder',prefer=1)
    process_TailLines(file_path,1,'Code size summary ')
    
    # 删除无用的log信息行
    process_Lines(file_path,1,0,r'^Time taken: \d+ ms ')
    process_Lines(file_path,1,0,r'^%s.+' % trunk)
    process_Lines(file_path,1,0,r'^In file included from .+')
    process_Lines(file_path,1,0,r'^                 from %s.+' % trunk)
    process_Lines(file_path,1,0,r'^ld: warning: The min-sizing binary cannot be generated.+')
    process_Lines(file_path,1,0,r'^Executing post-build step.+')
    process_Lines(file_path,1,0,r'^cc1plus.exe: warning: command line option.+')
    
    # 删除空白行
    delete_blank(file_path,1)
    
    #替换一些内容为变量的写法
    process_Lines(file_path,3,0,r'-o "%s(/.+)?/' % trunk,r'-o "%outdir%/') # 替换尾部的输出路径
    process_Lines(file_path,3,0,para_include,'%include%')
    process_Lines(file_path,3,0,para_hong,'%hong%')
    process_Lines(file_path,3,0,para_common,'%common1%')
    process_Lines(file_path,3,0,para_dir1,'%dir1%')
    process_Lines(file_path,3,0,para_dir2,'%dir2%')
    process_Lines(file_path,3,0,trunk,'%ttk%')
    
    # 直接添加新的头部
    process_HeadLines(file_path,4,'',new_head=new_head,prefer=1) #直接添加新头部
    process_Lines(file_path,3,0,para_name_part,'-DCONFIG_SYSTEM_VERSION="""%release_name%"""') #替换输出的bin名字
    
    #插入新的小尾巴
    insert_multi_lines(file_path,-2,new_tail)
    
    #完成一些小部分的替换
    process_Lines(file_path,3,0,para_A1,para_A2)
    process_Lines(file_path,3,0,para_B1,para_B2)
    process_Lines(file_path,3,0,para_C1,para_C2)
    
    f_line_number = getlines_num(file_path)
    process_Lines(file_path,3,1,r'%ttk%',r'%ttkb%',f_line_number-1,f_line_number)
    
    # 将所有文件的后缀更换为bat
    new_path = os.path.splitext(file_path)[0] + ".bat"
    os.rename(file_path, new_path)
